package utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AESEncryption {
    public static void main(String[] args) {
        String input = "123";

        try {
            String encrypted = encrypt(input);
            System.out.println("加密后的字符串： " + encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String encrypt(String input) throws Exception {
        String key = "1234567812345678"; // 密钥长度必须是16个字符
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }
}
